/**
 * 版权所有 @鸿名物联 
 * 未经授权，禁止侵权和商业，违法必究
 * 联系QQ：2224313811 
 * 
 */
package com.lp.controller.iot;

import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.lp.bo.IotNodeInfoBO;
import com.lp.bo.IotSceneInfoBO;
import com.lp.bo.IotSceneUserRelationBO;
import com.lp.bo.UserInfoBO;
import com.lp.cache.CacheName;
import com.lp.cache.ProCacheUtil;
import com.lp.common.Code;
import com.lp.common.Constants;
import com.lp.common.RequestURLIOT;
import com.lp.controller.BaseController;
import com.lp.service.IotSceneInfoService;
import com.lp.util.ObjectUtil;
import com.lp.util.ResultMapUtils;


@Controller
public class IotSceneInfoController extends BaseController {

	/**
	 * 检索
	 * @param response
	 * @param 
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, value = RequestURLIOT.SceneInfo.SCENE_INFO_PAGE)
	public ModelAndView selectPage(HttpServletResponse response, 
			@RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey ,
			@RequestBody IotSceneInfoBO obj,
			@RequestParam(required=false) Integer pageSize ,
			@RequestParam Integer paged ) {
		Map<String, Object> resultMap = getResultMap();
		try {
			UserInfoBO user = getUserInfoByUserKey(userKey);
			obj.setUser_id(user.getId());
			resultMap = service.selectPageList("IotSceneInfo.select",getPageBean(paged,pageSize), obj);								
		} catch (Exception e) {
			exception(e,resultMap, obj);
		}
		return getModelAndView(response, resultMap);
	}
	
	/**
	 * 超管使用检索
	 * @param response
	 * @param 
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, value = RequestURLIOT.SceneInfo.ADMIN_SCENE_INFO_PAGE)
	public ModelAndView selectALLPage(HttpServletResponse response, 
			@RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey ,
			@RequestBody IotSceneInfoBO obj,
			@RequestParam(required=false) Integer pageSize ,
			@RequestParam Integer paged ) {
		Map<String, Object> resultMap = getResultMap();
		try {
			UserInfoBO user = getUserInfoByUserKey(userKey);
			if(user.getType() == Code.UserType.SUPER ){
				resultMap = service.selectPageList("IotSceneInfo.select",getPageBean(paged,pageSize), obj);
			}
		} catch (Exception e) {
			exception(e,resultMap, obj);
		}
		return getModelAndView(response, resultMap);
	}
	
	
	
	
	/**
	 * 检索
	 * @param response
	 * @param 
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, value = RequestURLIOT.SceneInfo.SELF_SCENE_INFO_PAGE)
	public ModelAndView selectSelfPage(HttpServletResponse response, 
			@RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey ,
			@RequestBody IotSceneInfoBO obj,
			@RequestParam(required=false) Integer pageSize ,
			@RequestParam Integer paged ) {
		Map<String, Object> resultMap = getResultMap();
		try {
			UserInfoBO user = getUserInfoByUserKey(userKey);
			obj.setUser_id(user.getId());
			resultMap = service.selectPageList("IotSceneInfo.selectSceneInfo",getPageBean(paged,pageSize), obj);								
		} catch (Exception e) {
			exception(e,resultMap, obj);
		}
		return getModelAndView(response, resultMap);
	}
	
	/**
	 * 插入
	 * @param response
	 * @param 
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, value = RequestURLIOT.SceneInfo.SCENE_INFO)
	public ModelAndView save(HttpServletResponse response, 
			@RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey ,
			@RequestBody IotSceneInfoBO obj ) {
		Map<String, Object> resultMap = getResultMap();
		try {
			UserInfoBO user = getUserInfoByUserKey(userKey);
			if(ObjectUtil.isNotEmpty(user)){
				obj.setUser_id(user.getId());
				resultMap = service.insert("IotSceneInfo.insert", obj) ;
				if(isOk(resultMap)){
					// 场景插入成功，需要添加场景和用户关系
					IotSceneUserRelationBO tmp = new IotSceneUserRelationBO();
					tmp.setScene_id(obj.getId());
					tmp.setUser_id(user.getId());
					tmp.setAid(user.getId());
					resultMap = service.insert("IotSceneUserRelation.insert", tmp);
					if(isOk(resultMap)){
						// 添加场景缓存
						ProCacheUtil.addCache(CacheName.SCENEINFO, obj.getId().toString(), obj);
					}
				}				
			}else{
				putStatusCode(resultMap, Code.ResponseCode.SystemCode.NO_AUTHORIZATION);
			}
		} catch (Exception e) {
			exception(e,resultMap, obj);
		}
		return getModelAndView(response, resultMap);
	}
	
	/**
	 * 查询单个
	 * @param response
	 * @param id
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = RequestURLIOT.SceneInfo.SCENE_INFO)
	public ModelAndView selectOne(HttpServletResponse response, 
			@RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey ,
			@RequestParam Integer id ) {
		Map<String, Object> resultMap = getResultMap();
		try {
			IotSceneInfoBO obj = new IotSceneInfoBO();
			UserInfoBO user = getUserInfoByUserKey(userKey);
			if(ObjectUtil.isNotEmpty(user)  ){
				if(!verifyUserRole(userKey, Code.UserType.SUPER)){
					obj.setUser_id(user.getId());					
				}
				obj.setId(id);
				resultMap = service.selectOne("IotSceneInfo.selectOne", obj) ;				
			}else{
				putStatusCode(resultMap, Code.ResponseCode.SystemCode.ERROR);
			}
		} catch (Exception e) {
			exception(e,resultMap, id);
		}
		return getModelAndView(response, resultMap);
	}
	
	@Autowired
	private IotSceneInfoService iotSceneInfoservice ;
	
	/**
	 * 查询场景下详情
	 * @param response
	 * @param userKey
	 * @param id
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = RequestURLIOT.SceneInfo.SCENE_DETAILL)
	public ModelAndView selectOneDetail(HttpServletResponse response, 
			@RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey ,
			@RequestParam Integer id ) {
		Map<String, Object> resultMap = getResultMap();
		try {
			resultMap = iotSceneInfoservice.getSceneDetailInfo(new IotSceneInfoBO(id)) ;
		} catch (Exception e) {
			exception(e,resultMap, id);
		}
		return getModelAndView(response, resultMap);
	}
	
	/**
	 * 更新
	 * @param response
	 * @param obj
	 * @return
	 */
	@RequestMapping(method = RequestMethod.PUT, value = RequestURLIOT.SceneInfo.SCENE_INFO)
	public ModelAndView update(HttpServletResponse response, 
			@RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey ,
			@RequestBody IotSceneInfoBO obj ) {
		Map<String, Object> resultMap = getResultMap();
		try {
			UserInfoBO user = getUserInfoByUserKey(userKey);
			obj.setUser_id(user.getId());
			resultMap = service.update("IotSceneInfo.update", obj) ;
			if(isOk(resultMap)){
				// 添加场景缓存
				ProCacheUtil.addCache(CacheName.SCENEINFO, obj.getId().toString(), obj);
			}
		} catch (Exception e) {
			exception(e,resultMap, obj);
		}
		return getModelAndView(response, resultMap);
	}
	
	/**
	 * 删除
	 * @param response
	 * @param id
	 * @return
	 */
	@RequestMapping(method = RequestMethod.DELETE, value = RequestURLIOT.SceneInfo.SCENE_INFO)
	public ModelAndView delete(HttpServletResponse response, 
			@RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey ,
			@RequestParam Integer id ) {
		Map<String, Object> resultMap = getResultMap();
		try {
			IotSceneInfoBO obj = new IotSceneInfoBO();
			UserInfoBO user = getUserInfoByUserKey(userKey);
			if(ObjectUtil.isEmpty(id)){
				putStatusCode(resultMap, Code.ResponseCode.SystemCode.PARAM_ERROR);
			}else{
				// 判断场景下是否包含设备，如果包含，则提示场景下存在设备
				IotNodeInfoBO node = new IotNodeInfoBO() ;
				node.setScene_id(id);
				Integer deviceNum = service.count("IotNodeInfo.selectPageCount", node ) ;
				if(deviceNum != null && deviceNum <= 0 ){
					obj.setId(id);
					obj.setUser_id(user.getId());
					obj.setDelete_flag(Constants.DELETE.YES);
					resultMap = service.update("IotSceneInfo.update", obj) ;	
					if(isOk(resultMap)){
						ProCacheUtil.removeCache(CacheName.SCENEINFO, obj.getId().toString());
						// 这边需要删除，所有的关联
						IotSceneUserRelationBO iotSceneUser = new IotSceneUserRelationBO() ;
						iotSceneUser.setScene_id(id);
						service.delete("IotSceneUserRelation.deleteUserSceneRelation", iotSceneUser) ;
					}					
				}else{
					putStatusCode(resultMap, Code.ResponseCode.SystemCode.ERROR , "场景下存在设备，不能删除场景") ;
				}
			}
		} catch (Exception e) {
			exception(e,resultMap, id);
		}
		return getModelAndView(response, resultMap);
	}
}
